package com.hifar.test.oaApi.service.impl;

import com.hifar.fw.utils.bean.BusiRequest;
import com.hifar.fw.utils.lang.StringUtils;
import com.hifar.oldPlatform.utils.SysCommonUtil;
import com.hifar.proj.sysSwitch.service.IBaseSysSwitchService;
import com.hifar.test.oaApi.entity.OaSystemConfig;
import com.hifar.test.oaApi.service.OaSystemConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * OA系统配置服务实现类（临时实现）
 * TODO: 后续需要从数据库或配置文件中读取配置
 *
 * @author zhuWeiWei
 * @date 2025/10/29
 */
@Service
public class OaSystemConfigServiceImpl implements OaSystemConfigService {

    private static final Logger log = LoggerFactory.getLogger(OaSystemConfigServiceImpl.class);

    @Autowired
    private IBaseSysSwitchService baseSysSwitchService;

    /**
     * 获取OA系统配置
     * 从系统配置表读取OA相关配置
     *
     * @return OA系统配置信息
     */
    @Override
    public OaSystemConfig getOaSystemConfig() {
        try {
            log.info("开始加载OA系统配置...");

            OaSystemConfig oaSystemConfig = new OaSystemConfig();

            // 读取配置项
            String oaTaskEnable = baseSysSwitchService.getSysConfigValueByKey("oa_task_enable");
            String oaAccessKey = baseSysSwitchService.getSysConfigValueByKey("oa_access_key");
            String oaSecret = baseSysSwitchService.getSysConfigValueByKey("oa_secret");
            String applicationName = baseSysSwitchService.getSysConfigValueByKey("oa_application_name");
            String oaServerUrl = baseSysSwitchService.getSysConfigValueByKey("oa_server_url");
            String oaSsoUrl = baseSysSwitchService.getSysConfigValueByKey("oa_sso_url");

            // 设置配置项（使用默认值处理null）
            oaSystemConfig.setIsEnable(Boolean.parseBoolean(StringUtils.trimNull(oaTaskEnable, "false")));
            oaSystemConfig.setAccessKey(StringUtils.trimNull(oaAccessKey));
            oaSystemConfig.setSecret(StringUtils.trimNull(oaSecret));
            oaSystemConfig.setApplicationName(StringUtils.trimNull(applicationName, "试验系统"));
            oaSystemConfig.setServerUrl(StringUtils.trimNull(oaServerUrl));
            oaSystemConfig.setSsoUrl(StringUtils.trimNull(oaSsoUrl));

            log.info("OA系统配置加载完成 - 启用状态: {}, 服务器地址: {}, 应用名称: {}",
                     oaSystemConfig.getIsEnable(),
                     oaSystemConfig.getServerUrl(),
                     oaSystemConfig.getApplicationName());

            return oaSystemConfig;
        } catch (Exception e) {
            log.error("加载OA系统配置异常: {}", e.getMessage(), e);
            // 返回一个禁用状态的配置对象，避免返回null
            OaSystemConfig disabledConfig = new OaSystemConfig();
            disabledConfig.setIsEnable(false);
            return disabledConfig;
        }
    }
}
